package com.sgxy.aclservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sgxy.aclservice.entity.AclDept;
import com.sgxy.aclservice.entity.AclEmp;
import com.sgxy.aclservice.entity.vo.AclDeptVo;
import com.sgxy.aclservice.entity.vo.OneDept;
import com.sgxy.aclservice.entity.vo.TwoDept;
import com.sgxy.aclservice.mapper.AclDeptMapper;
import com.sgxy.aclservice.service.AclDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgxy.aclservice.service.AclEmpService;
import com.sgxy.servicebase.exceptionhandler.ERPException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 部门 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-27
 */
@Service
public class AclDeptServiceImpl extends ServiceImpl<AclDeptMapper, AclDept> implements AclDeptService {

    @Autowired
    private AclEmpService aclEmpService;
    @Override
    public boolean deleteDeptById(String id) {
        QueryWrapper<AclEmp> wrapper = new QueryWrapper<>();
        wrapper.eq("dept_id",id);
        int count = aclEmpService.count(wrapper);
        if(count>0){
            throw new ERPException(20001,"该部门正被使用，无法删除");
        }

        baseMapper.deleteById(id);
        return true;
    }

    @Override
    public List<OneDept> getAllOneTwoDept() {
        //查询一级分类 pid=0
        QueryWrapper<AclDept> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("pid",0);
        //使用父类中的mapper对象查询数据库
        List<AclDept> aclDeptOne = baseMapper.selectList(wrapperOne);

        //查询二级分类 pid!=0;
        QueryWrapper<AclDept> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("pid",0);
        //使用父类中的mapper对象查询数据库
        List<AclDept> aclDeptTwo = baseMapper.selectList(wrapperTwo);

        List<OneDept> list = new ArrayList<>();
        Map<String,List<TwoDept>> map = new HashMap<>();
        //封装二级
        List<TwoDept> twoDepts;
        for(AclDept aclDept:aclDeptTwo){
            TwoDept twoDept = new TwoDept();
            //调用spring的工具类，将一个对象的值赋予另一个对象，相对于oneSubject.setId(eduSubject.getId())
            BeanUtils.copyProperties(aclDept,twoDept);
            if(map.containsKey(aclDept.getPid())){
                map.get(aclDept.getPid()).add(twoDept);
            }else{
                ArrayList<TwoDept> twolist = new ArrayList<>();
                twolist.add(twoDept);
                map.put(aclDept.getPid(),twolist);
            }

        }

        //封装一级分类
        for(AclDept aclDept:aclDeptOne){
            OneDept oneDept = new OneDept();
            BeanUtils.copyProperties(aclDept,oneDept);
            List<TwoDept> twoDeptList = map.get(aclDept.getId());
            oneDept.setChildren(twoDeptList);
            list.add(oneDept);
        }
        return list;
    }

    @Override
    public IPage<AclDeptVo> selectPageVoList(IPage<AclDeptVo> page, AclDept aclDept) {
        return baseMapper.selectPageVoList(page,aclDept);
    }

    @Override
    public AclDeptVo selectOneById(String id) {
        return baseMapper.selectOneById(id);
    }
}
